﻿<%@ WebHandler Language="C#" Class="GallaryHandler" %>
     
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using Net.Ntalent.Action50.Core.Service;
using Net.Ntalent.Action50.Core.Util;

/// <summary>
///  负责处理相册图片的上传和获取工作的类，实现IHttpHandler接口。
/// </summary>
/// <author>王彦博</author>
public class GallaryHandler : IHttpHandler
{
    /// <summary>
    /// 处理器默认处理方法，在此方法中将根据获取的动作参数分派特定的方法来作处理。
    /// </summary>
    /// <param name="context">
    /// http请求的上下文信息
    /// </param>
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string action = context.Request["action"];
        switch (action)
        {
            case "uploadImage":
                context.Response.Write(this.UploadImage(context));
                break;
            case "getImage":
                byte[] imageArray = this.GetImage(context);
                context.Response.ContentType = "image/jpeg";
                context.Response.OutputStream.Write(imageArray, 0, imageArray.Length);
                context.Response.Flush();
                break;
            case "getLogo"://如果未上传logo，则给出一张默认的
                long actionId = Convert.ToInt64(context.Request["actionId"]);
                long logoId = Helper.getAvatarIdByAction(actionId);
                byte[] logo = null;
                if (logoId == 0)
                {
                    string path = context.Server.MapPath("~/images/DefaultLogo.png");
                    Image image = Image.FromFile(path);
                    MemoryStream ms = new MemoryStream();
                    image.Save(ms, ImageFormat.Png);
                    logo = ms.ToArray();                 
                }
                else
                {
                    logo = new GallaryService().Retrieve(logoId).UPLOAD_FILE_DATA.ToArray();
                }
                context.Response.ContentType = "image/jpeg";
                context.Response.OutputStream.Write(logo, 0, logo.Length);
                context.Response.Flush();
                break;              
        }
    }

    /// <summary>
    /// 接口HttpHandler必须实现的属性，
    /// </summary>
    public bool IsReusable
    {
        get { return false; }
    }

    /// <summary>
    /// 在此方法中获取数据库中的图片数据，直接写出到页面中。
    /// </summary>
    /// <param name="context">
    /// http请求的上下文信息
    /// </param>
    private byte[] GetImage(HttpContext context)
    {
        //获取需要的获取的图片id
        long gallaryId = Convert.ToInt64(context.Request["id"]);

        // 声明返回对象
        byte[] resultArray = null;

        // 调用GallaryService获取gallary数据。
        using (var gallaryService = new GallaryService())
        {
            resultArray = gallaryService.GetImage(gallaryId);
        }
        return resultArray;
    }

    /// <summary>
    ///上传图片
    /// </summary>
    /// <param name="context">
    /// http请求的上下文信息
    /// </param>
    public string UploadImage(HttpContext context)
    {
        HttpFileCollection files = context.Request.Files;
        if (files.Count > 0)
        {
            HttpPostedFile file = files[0];
            string gallaryDesc = context.Request["gallaryDesc"];
            int gallaryType = Convert.ToInt32(context.Request["gallaryType"]);
            long actionId = Convert.ToInt64(context.Request["actionId"]);
            using (var service = new GallaryService())
            {
                service.UpLoadImage(file, gallaryDesc, gallaryType, actionId, context.Request.UserHostAddress);
            }
        }
        return "1";
    }
}
